package com.gitee.wsl.ext.list

import kotlin.jvm.JvmName

/**
 * Adds the given [elements] to the start of the [MutableList]
 *
 * @return the same instance
 */
fun <T> MutableList<T>.unshift(vararg elements: T): MutableList<T> = unshift(elements)

/**
 * Adds the given [elements] to the start of the [MutableList]
 *
 * @return the same instance
 */
@JvmName("unshiftArray")
fun <T> MutableList<T>.unshift(elements: Array<out T>): MutableList<T> = unshift(elements.toList())

/**
 * Adds the given [elements] to the start of the [MutableList]
 *
 * @return the same instance
 */
@JvmName("unshiftCollection")
fun <T> MutableList<T>.unshift(elements: Collection<T>): MutableList<T> = apply { addAll(0, elements) }

/**
 * Removes and returns the first element of the [MutableList]
 *
 * If there is no entry in the list then null is returned.
 */
fun <T> MutableList<T>.shift(): T? = if (isNotEmpty()) removeAt(0) else null
